package great.easychat.help.fragment

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import great.easychat.help.BaseFragment
import great.easychat.help.CustomAppcation
import great.easychat.help.R
import great.easychat.help.WebUrlActivity
import great.easychat.help.activity.PermissonSettingActivity
import great.easychat.help.activity.SuggestActivity
import great.easychat.help.dialog.CommonDialog
import great.easychat.help.dialog.ShareDialogFragment
import great.easychat.help.uinque.UUIDUtils
import great.easychat.help.util.*
import great.easychat.help.viewModel.SplashViewModel
import kotlinx.android.synthetic.main.fragment_me.*


// 我的
class MeFragment : BaseFragment<SplashViewModel>() {

    override fun requestLayoutId(): Int {
        return R.layout.fragment_me
    }

    override fun observeLiveData() {
        /**
         * 获取配置
         * 检查是否已经注册
         * 进行注册
         * 登录并缓存用户信息
         */
        viewModel.hostConfigLiveData.observe(this, Observer { configBean ->
            UserInfoManger.config = configBean
            if (!UpdateHelper.startCheck(activity)) {
                ToastUtil.show("当前已是最新版本")
            }
        })
    }

    override fun setViewData(savedInstanceState: Bundle?) {

    }

    override fun initView(rootView: View?) {

    }

    override fun initData(argments: Bundle?){
//
        tvWechat.text = "客服微信 " + UserInfoManger.config.we_chat
        tvVersion.text = "v" + Util.getAppVersionName(context)
        tvInviteId.text = "用户ID: " + UserInfoManger.getUserInfo().userName
        tvCopy.setOnClickListener {
            Util.setClipboard(context,UserInfoManger.getUserInfo().userName)
            ToastUtil.show("用户ID复制成功")
        }
        if (UserInfoManger.config.getAuditMode() == 1) {
            llMoney.visibility=View.GONE
        } else {
            llMoney.visibility=View.VISIBLE
        }
        val lastVer = UserInfoManger.config.version
        val currVer = Integer.valueOf(Util.getAppVersionCode(context))
        if (lastVer > currVer) {
            ivVersion.visibility = View.VISIBLE
        } else {
            ivVersion.visibility = View.GONE
        }

        llPermission.setOnClickListener {
            startActivity(Intent(activity, PermissonSettingActivity::class.java))
        }

        llSuggest.setOnClickListener {
            startActivity(Intent(activity, SuggestActivity::class.java))
        }

        llShare.setOnClickListener {
            val shareDialogFragment = ShareDialogFragment()
            shareDialogFragment.show(fragmentManager!!)
        }

        llService.setOnClickListener {
//            Util.setClipboard(context,UserInfoManger.config.data.data.we_chat)
            val commonDialog = CommonDialog()
            commonDialog.setContent("客服微信已复制，是否打开微信添加为好友")
            commonDialog.show(activity, object : CommonDialog.Callback {
                override fun sure() {
                    openWechat()
                }

                override fun cancel() {}
            })
        }

        llPro.setOnClickListener {
            WebUrlActivity.start(activity, "http://www.chattinghelper.cn/index.php?m=home&c=View&a=index&aid=70&admin_id=1&t=1610867700", "用户注册协议")
        }

        llpri.setOnClickListener {
            WebUrlActivity.start(activity, "http://www.chattinghelper.cn/index.php?m=home&c=View&a=index&aid=69&admin_id=1&t=1610867700", "隐私协议")
        }

        llMoney.setOnClickListener {
            if (hasInstalledAlipayClient(context!!)) {
                Util.donate(context, "https://qr.alipay.com/FKX0472769PXFUNLJEUXF86")
            } else {
                ToastUtil.show("您未安装支付宝")
            }
        }

        llVersion.setOnClickListener {
            viewModel.getConfig()
        }

    }
    fun openWechat() {
        if (CustomAppcation.isWeChatAppInstalled()) {
            val intent = Intent()
            val cmp = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
            intent.action = Intent.ACTION_MAIN
            intent.addCategory(Intent.CATEGORY_LAUNCHER)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.component = cmp
            startActivity(intent)
        } else {
            ToastUtil.show("您需要安装微信客户端")
        }
    }

    // 支付宝包名
    private val ALIPAY_PACKAGE_NAME = "com.eg.android.AlipayGphone"

    /**
     * 判断支付宝客户端是否已安装，建议调用转账前检查
     * @param context Context
     * @return 支付宝客户端是否已安装
     */
    fun hasInstalledAlipayClient(context: Context): Boolean {
        val pm = context.packageManager
        return try {
            val info = pm.getPackageInfo(ALIPAY_PACKAGE_NAME, 0)
            info != null
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            false
        }
    }
}
